package cdu.javaendwork.service.Impl.AdoptionMore;


import cdu.javaendwork.dao.Universal;
import cdu.javaendwork.dao.impl.UniversalImpl;
import cdu.javaendwork.model.sqlModel.Post;
import cdu.javaendwork.service.Interface.AdoptionMore.PostService;

import java.util.Date;
import java.util.List;

public class PostServiceImpl implements PostService {
    Universal universal = new UniversalImpl();

    @Override
    public boolean insert(int userid, int petid, int status, String title, String content, Date date) {
        return universal.insert("Post", userid,  petid,  status,  title,  content,  date)==1;
    }

    @Override
    public boolean update(int id ,int userid, int petid, int status, String title, String content, Date date) {
        return universal.updateById("Post",id,userid,petid,status,title,content,date)==1;
    }

    @Override
    public Post findByid(int id) {
        return universal.getById("Post",id);
    }

    @Override
    public List<Post> Postlimit(int pageIndex, int pageSize) {
        return universal.filterLimit("Post","status=1",pageIndex,pageSize);
    }

    @Override
    public List<Post> filterByTitleAndContentLimit(String where, int pageIndex, int pageSize) {
        String sql="status=1 and (title like '%" + where + "%' or content like '%"+where+"%' )";
        System.out.println(sql);
        return universal.filterLimit("Post",sql,pageIndex,pageSize);
    }

    @Override
    public List<Post> AdminPostLimit(int pageIndex, int pageSize) {
        return universal.limit("Post",pageIndex,pageSize);
    }

    @Override
    public List<Post> AdminFilterByTitleAndContentLimit(String where, int pageIndex, int pageSize) {
        String sql="(title like '%" + where + "%' or content like '%"+where+"%' )";
//        System.out.println(sql);
        return universal.filterLimit("Post",sql,pageIndex,pageSize);
    }

    @Override
    public List<Post> filterByUserIdLimit(int userid, int pageIndex, int pageSize) {
        return universal.filterLimit("Post","status=1 and userid="+userid,pageIndex,pageSize);
    }
    @Override
    public List<Post> filterByTitleAndContentAndUserIdLimit(String where, int userid,int pageIndex, int pageSize) {
        String sql="status=1 and (title like '%" + where + "%' or content like '%"+where+"%' ) and (userid="+userid+")";
//        System.out.println(sql);
        return universal.filterLimit("Post",sql,pageIndex,pageSize);
    }
}
